package lumingweihua.future.cn.lumingweihua.home;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import lumingweihua.future.cn.baselibgxh.base.BaseFragment;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.network.AbstractRequest;
import lumingweihua.future.cn.baselibgxh.network.BeanRequest;
import lumingweihua.future.cn.baselibgxh.network.NetworkManager;
import lumingweihua.future.cn.baselibgxh.network.ReqTag;
import lumingweihua.future.cn.baselibgxh.network.entity.MamaHaoServerError;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.home.cheyuan.CheyuanFragment;
import lumingweihua.future.cn.lumingweihua.home.cheyuan.HuoyuanFragment;
import lumingweihua.future.cn.lumingweihua.home.domain.MessageData;
import lumingweihua.future.cn.lumingweihua.ui.message.MessageActivity;

/**
 * Created by Administrator on 2018/3/2.
 */

public class HomeFragment extends BaseFragment {
    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        return View.inflate(getActivity(), R.layout.fragment_home, null);
    }

    private Fragment[] fragmentPages;
    private TextView[] tv_tabs;
    private View[] divides;
    private HuoyuanFragment huoyuanFragment;
    private CheyuanFragment cheyuanFragment;
    @Override
    protected void initData(Bundle savedInstanceState) {
        findView(R.id.ll_01).setOnClickListener(this);
        findView(R.id.ll_02).setOnClickListener(this);
        findView(R.id.iv_message).setOnClickListener(this);

        cheyuanFragment = new CheyuanFragment();
        huoyuanFragment = new HuoyuanFragment();
        fragmentPages = new Fragment[]{huoyuanFragment,cheyuanFragment};

        getChildFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragmentPages[0]).commit();

        tv_tabs = new TextView[]{findTextView(R.id.tv_01),findTextView(R.id.tv_02)};
        divides = new View[]{findView(R.id.title_divide01),findView(R.id.title_divide02)};

        requestMsgs();
    }

    public void refresh(int index){
        if(index == 0)
            huoyuanFragment.refreshDatas(true,true);
        else
            cheyuanFragment.refreshDatas(true,true);
    }

    /** 请求标识 **/
    private final String REQUEST_TAG = "HomeFragment";
    private final int REQUEST_MESGS = 0;
    private void requestMsgs() {
        // TODO Auto-generated method stub
        BeanRequest<MessageData, MamaHaoServerError> req = new BeanRequest<>(
                context,
                "",
                this,
                MessageData.class);

        req.setParam("apiCode", "_shuliangs_001");
        req.setParam("userToken", MyApplication.getInstance().getUserToken());

        //获取token
        String token = NetworkManager.getInstance().getPostToken(req.getParam());
        req.setParam("token", token);

        baseActivity.addRequestQueue(req, REQUEST_MESGS, new ReqTag.Builder().tag(REQUEST_TAG));

    }

    @Override
    protected void onResponseSuccess(ReqTag tag, Object data) {
        super.onResponseSuccess(tag, data);
        if(!REQUEST_TAG.equals(tag.getTag()))
            return;

        switch (tag.getReqId()) {
            case REQUEST_MESGS:
                //普通商品列表
                MessageData md = (MessageData)data;
                if(md != null && (md.getCarriage_num()+md.getMessage_num()>0)) {
                    //有未读消息
                    findView(R.id.view_dot).setVisibility(View.VISIBLE);
                }else{
                    findView(R.id.view_dot).setVisibility(View.GONE);
                }
                break;

            default:
                break;
        }
    }

    @Override
    protected void onResponseFailure(ReqTag tag, MamaHaoServerError error, AbstractRequest.MamaHaoError clientError) {
        super.onResponseFailure(tag, error, clientError);
    }

    /**
     * 蒙成是否显示
     * @return
     */
    public boolean isShowingMask(){
        return huoyuanFragment.view_mask!=null && huoyuanFragment.view_mask.getVisibility() == View.VISIBLE;
    }

    public void dismissMask(){
        huoyuanFragment.dismissMask();
    }

    @Override
    protected void processClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.ll_01:
                changeTab(0);
                break;
            case R.id.ll_02:
                changeTab(1);
                break;
            case R.id.iv_message:
                //消息
                baseActivity.jumpToNextActivity(MessageActivity.class,false);
                shouldRefresh = true;
                break;

            default:
                break;
        }
    }

    private boolean shouldRefresh;

    @Override
    public void onResume() {
        super.onResume();
        if(shouldRefresh){
            shouldRefresh = false;
            requestMsgs();
        }
    }

    private int currentTabIndex;

    private void changeTab(int index){
        if(currentTabIndex == index)
            return;

//        tv_tabs[currentTabIndex].setTextColor(Color.parseColor("#333333"));
        divides[currentTabIndex].setVisibility(View.GONE);

//        tv_tabs[index].setTextColor(Color.parseColor("#006de3"));
        divides[index].setVisibility(View.VISIBLE);

        FragmentTransaction ft = getChildFragmentManager()
                .beginTransaction();
        ft.hide(fragmentPages[currentTabIndex]);
        if (!fragmentPages[index].isAdded()) {
            ft.add(R.id.fragment_container, fragmentPages[index]);
        }
        ft.show(fragmentPages[index]).commit();

        currentTabIndex = index;

    }

}
